package cn.edu.njust.controller;

import cn.edu.njust.pojo.bo.PageBean;
import cn.edu.njust.pojo.bo.RestResult;
import cn.edu.njust.pojo.dto.PageQueryDTO;
import cn.edu.njust.pojo.po.User;
import cn.edu.njust.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 超级管理员功能控制器
 *
 * @author wangs
 */
@Tag(name = "超级管理员功能接口")
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/root")
public class RootController {

    private final UserService userService;

    /**
     * 分页查询学生信息
     *
     * @param pageQuery 分页参数
     */
    @Operation(summary = "分页查询学生信息")
    @PostMapping("/selectByPage")
    public RestResult<PageBean<User>> selectByPage(@RequestBody PageQueryDTO pageQuery) {
        PageBean<User> pageBean = userService.selectUserList(pageQuery.getPageNum(), pageQuery.getPageSize(), pageQuery.getUser());
        return RestResult.success(pageBean);
    }

    /**
     * 批量删除
     *
     * @param ids id列表
     */
    @Operation(summary = "批量删除")
    @PostMapping("/deleteByIds")
    protected RestResult<Void> deleteByIds(@RequestBody List<Integer> ids) {
        userService.deleteByIds(ids);
        return RestResult.success();
    }

    /**
     * 根据id删除用户
     *
     * @param id 用户id
     */
    @Operation(summary = "根据id删除用户")
    @PostMapping("/deleteById")
    public RestResult<Void> deleteById(@RequestParam("id") Integer id) {
        userService.deleteByIds(Collections.singleton(id));
        return RestResult.success();
    }

    /**
     * 重置用户密码
     *
     * @param ids 用户id列表
     */
    @Operation(summary = "重置用户密码")
    @PostMapping("/resetPassword")
    public RestResult<Void> resetPassword(@RequestParam("ids") List<Integer> ids) {
        userService.resetPasswordByIds(ids);
        return RestResult.success();
    }
}
